package com.ian.departmentmanager.mvc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ian.departmentmanager.bo.Member;
import com.ian.departmentmanager.service.MemberService;

@Controller
public class MemberInfoController {

	private MemberService memberSerivce;

	@Autowired
	public void setMemberSerivce(MemberService memberSerivce) {
		this.memberSerivce = memberSerivce;
	}

	@RequestMapping(value = "/member/index.htm", method = RequestMethod.GET)
	protected ModelAndView index() {
		ModelAndView mav = new ModelAndView("memberInfoIndex");
		List<Member> members = memberSerivce.getMembers();
		mav.addObject("members", members);
		return mav;
	}

	@RequestMapping(value = "/member/delete.htm", method = RequestMethod.GET)
	protected ModelAndView delete(
			@RequestParam(value = "Id", required = true) int Id,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("memberInfoIndex");
		memberSerivce.delMember(Id);
		List<Member> members = memberSerivce.getMembers();
		mav.addObject("members", members);
		return mav;
	}

	@RequestMapping(value = "/member/update.htm", method = RequestMethod.GET)
	protected ModelAndView update(
			@RequestParam(value = "Id", required = true) int Id,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("memberInfo");
		Member mem = memberSerivce.getMember(Id);
		mav.addObject("member", mem);
		return mav;
	}

	@RequestMapping(value = "/member/updateProcess.htm", method = RequestMethod.POST)
	protected String updateProcess(
			@RequestParam(value = "Id") int Id,
			@RequestParam(value = "stuID") String stuID,
			@RequestParam(value = "stuName") String stuName,
			@RequestParam(value = "contact") String contact,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "profession") String profession,
			HttpServletRequest request) {
		
		memberSerivce.updateMember(Id, stuName, stuID, profession, contact, email);
		
		return "redirect:/member/index.htm";
	}
	
	@RequestMapping(value = "/member/insert.htm", method = RequestMethod.GET)
	protected ModelAndView insert() {
		
		ModelAndView mav = new ModelAndView("memberInfoAdd");
		return mav;
	}

	@RequestMapping(value = "/member/insertProcess.htm", method = RequestMethod.POST)
	protected String insertProcess(
			@RequestParam(value = "stuID") String stuID,
			@RequestParam(value = "stuName") String stuName,
			@RequestParam(value = "contact") String contact,
			@RequestParam(value = "email") String email,
			@RequestParam(value = "profession") String profession,
			HttpServletRequest request) {
		
		memberSerivce.addMember(stuName, stuID, profession, contact, email);
		
		return "redirect:/member/index.htm";
	}
}
